﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using Emgu.CV;
using Emgu.CV.Structure;
using paravili.Services;
using paravili.Steps;
using Sebarf.Diagnostics.Interfaces;

#endregion

namespace paravili.CaptureSources {
	/// <summary>
	/// Represents a capture source that gets the images data from a connected webcam
	/// </summary>
	public class WebCamBasedCaptureSource : ICaptureSource {
		#region Public Properties

		[ConfigurableNumericValue(Name = "Scale Factor", RangeFrom = 0, RangeTo = 3)]
		public int ScaleFactor { get; set; }

		#endregion

		#region Public Methods

		public WebCamBasedCaptureSource() {
			try {
				Name = "Webcam";
				ScaleFactor = 1;
				m_capture = new Capture();
			}
			catch {
				Logger.WriteInformation("No webcam has been found");
			}
		}

		#endregion

		#region Private Fields

		private readonly ICapture m_capture;

		#endregion

		#region ICaptureSource Members

		[ConfigurabelStringValue(Name = "Name of the datasource")]
		public string Name { get; set; }

		public Image<Bgr, byte> QueryImage() {
			Image<Bgr, byte> toReturn = m_capture.QueryFrame();
			for (int i = 0; i < ScaleFactor; i++) {
				if (toReturn == null) {
					return null;
				}
				toReturn = toReturn.PyrDown();
			}
			return toReturn;
		}

		public bool IsAvailable {
			get { return m_capture != null; }
		}

		#endregion
	}
}